define([
	'dojo/_base/declare', 'dojo/_base/lang',
	//'js/layer/County', 'js/layer/CityBorder', 'js/layer/ExpressWay', 'js/layer/CountryRoad', 'js/layer/RailWay', 'js/layer/CountyPosition', 
	'js/layer/Vector', 'dojo/Evented', 'dojo/request'
], function(
	declare, lang,
	//County, CityBorder, ExpressWay, CountryRoad, RailWay, CountyPosition, 
	Vector, Evented, request
) {
	return declare('js/Map', [Evented], {
		
		constructor: function(domId, opt) {
			//this.mapEvents = ['click','singleclick','dbclick','moveend','pointerdrag','pointermove','postcompose','postrender','precompose','propertychange'];
			this.mapEvents = ['click'];
			this.layers = [];
			this.map = new ol.Map({
				layers: [
					new ol.layer.Tile({
						source: new ol.source.XYZ({
							url: 'http://www.google.cn/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i345013117!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0'
						})
					})
				],
				view: new ol.View({
					center: [113.6, 34.1],
					projection: 'EPSG:4326',
					//center: this.lonLat2WebMercator(113.6, 34.1),
					//zoom: 4,
					zoom: 7
				}),
				target: domId
			});
//			this.on('layersloaded', lang.hitch(this, function() {
//				console.log(this.getLayerById('county'))
//			}));
			
			this.dispatchEvents();
			
			//console.log(this.map.getLayers());
		},
		
		//根据id获取图层对象
		getLayerById: function(id) {
			var layers = this.layers;
			for(var layer in layers) {
				if(layers[layer].id === id) return layers[layer];
			}
		},
		
		//加载图层
		loadLayers: function() {
			//var county = new County();
			//var cityBorder = new CityBorder();
			//var expressWay = new ExpressWay();
			//var countryRoad = new CountryRoad();
			//var railWay = new RailWay();
			//var countyLabel = new CountyPosition();
			//this.addLayer(county.layer);
			//this.addLayer(cityBorder.layer);
			//this.addLayer(expressWay.layer);
			//this.addLayer(countryRoad.layer);
			//this.addLayer(railWay.layer);
			//this.addLayer(countyLabel.layer);
			var urls = null;
			var self = this;
			
			request('/Map/src/UrlConfig.json').then(
					function(json) {
						urls = $.parseJSON(json);
						
						var layerConfig = [
						   			    {
						   			    	type: 'polygon', id: 'county', name: '县级市', url: urls.county
						   			    },
						   			    {
						   			    	type: 'line', id: 'cityBorder', name: '地级市边界', url: urls.cityBorder
						   			    },
						   			    {
						   			    	type: 'line', id: 'expressWay', name: '高速公路', url: urls.expressWay
						   			    },
						   			    {
						   			    	type: 'line', id: 'countryRoad', name: '国道', url: urls.countryRoad
						   			    },
						   			    {
						   			    	type: 'line', id: 'railWay', name: '铁路', url: urls.railWay
						   			    },
						   			    {
						   			    	type: 'point', id: 'countyLabel', name: '标注', url: urls.countyLabel
						   			    }
						   			];
						
						for(var i = 0; i < layerConfig.length; i++) {
							self.pushLayer(layerConfig[i]);
						}
						self.emit('layersloaded');
						
					
					},
					function(error) {
						console.log(error);
					}
			);
		},
		
		//this.layers集合中添加图层
		pushLayer: function(config) {
			var layer = new Vector(this.map, config);
			layer.initFeatures();
			this.layers.push(layer);
			this.addLayer(layer);
			this.emit('layerpushed',{layer: layer});
			return layer;
		},
		
		getView: function() {
			return this.map.getView();
		},

		addLayer: function(layer) {
			this.map.addLayer(layer.layer);
		},
		
		dispatchEvents: function() {
			var events = this.mapEvents;
			for(var i in events) {
				this.map.on(events[i], lang.hitch(this, function(evt) {
					var feature = null;
					var prop = null;
					if(evt.originalEvent) {
						var pixel = this.map.getEventPixel(evt.originalEvent);
						var flayer = this.map.forEachFeatureAtPixel(pixel, function(feature, layer) {
							if(layer) {
								var viewonly = layer.get('viewonly');
								if(!viewonly) {
									return {feature: feature, layer: layer};
								}
							}
						});
						if(!flayer) return;
						var feature = flayer.feature;
						if(feature && feature.getProperties) {
							prop = feature.getProperties();
						}
					}
					this.emit(events[i], {
						prop: prop,
						feature: feature,
						scope: this,
						evt: evt
					});
				}));
			}
		},
		
		//回到初始位置
		fullExtent: function() {
			var pan = new ol.animation.pan({
				duration: 500,
				source: this.map.getView().getCenter()
			});
			var zoom = new ol.animation.zoom({
				duration: 500,
				resolution: this.map.getView().getResolution()
			});
			this.map.beforeRender(pan);
			this.map.getView().setCenter([113.6, 34.1]);
			this.map.beforeRender(zoom);
			this.map.getView().setZoom(7);
		},

		//经纬度转Web墨卡托  
		lonLat2WebMercator: function(x,y){  
	        var xx = x *20037508.34/180; 
	        var yy = Math.log(Math.tan((90+y)*Math.PI/360))/(Math.PI/180);
	        yy = yy *20037508.34/180;  
	        return [xx,yy];  
	    },
	    //Web墨卡托转经纬度  
	    WebMercator2lonLat: function(x,y){  
	        var xx = x/20037508.34*180;  
	        var yy = y/20037508.34*180;  
	        yy= 180/Math.PI*(2*Math.atan(Math.exp(yy*Math.PI/180))-Math.PI/2);
	        return [xx,yy];
	    }  
	});
});